//
//  KKNavigationController.m
//  TS
//
//  Created by Coneboy_K on 13-12-2.
//  Copyright (c) 2013年 Coneboy_K. All rights reserved.  MIT
//  WELCOME TO MY BLOG  http://www.coneboy.com
//


#import "BaseNvc.h"
#import <QuartzCore/QuartzCore.h>
#import <math.h>
#import "config.h"
#import "MainViewController.h"
#import "CustomMoviePlayerViewController.h"



@interface BaseNvc ()<UINavigationControllerDelegate>
{
    CGPoint startTouch;
    
    UIImageView *lastScreenShotView;
    UIView *blackMask;
    
}

@property (nonatomic,retain) UIView *backgroundView;
@property (nonatomic,retain) NSMutableArray *screenShotsList;
@property (nonatomic,retain) UIPanGestureRecognizer *recognizer;
@property (nonatomic,assign) BOOL isMoving,canDragBack;

@end

@implementation BaseNvc
@synthesize recognizer,canDragBack,isMoving,backgroundView,screenShotsList;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.screenShotsList = [[NSMutableArray alloc]init];
        [self setNvcCanDragBack:YES];
        self.delegate = self;
    }
    return self;
}


-(void)setNvcCanDragBack:(BOOL)enble{
    self.canDragBack = enble;
    
    if (self.canDragBack==false) {
        
        [self.view removeGestureRecognizer:recognizer];
        recognizer = nil;
        
        
    }else{
        
        if (recognizer==nil) {
            recognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self
                                                                action:@selector(paningGestureReceive:)];
            [recognizer delaysTouchesBegan];
        }
        
        [self.view addGestureRecognizer:recognizer];
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImageView *shadowImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"leftside_shadow_bg"]];
    shadowImageView.frame = CGRectMake(-10, 0, 10, self.view.frame.size.height);
    [self.view addSubview:shadowImageView];
}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [self setNvcCanDragBack:NO];
    
    [self.screenShotsList addObject:[self capture]];
    
    [super pushViewController:viewController animated:animated];
}



- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    [self.view removeGestureRecognizer:recognizer];
    [self.screenShotsList removeLastObject];
    
    return [super popViewControllerAnimated:animated];
}




#pragma mark - Utility Methods -
- (UIImage *)capture
{
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return img;
}


- (void)moveViewWithX:(float)x
{
    
    CGRect frame = self.view.frame;
    if (!self.backgroundView){
        
        self.backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width , frame.size.height)];
        
        [self.view.superview insertSubview:self.backgroundView belowSubview:self.view];
        
        blackMask = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width , frame.size.height)];
        blackMask.backgroundColor = [UIColor blackColor];
        [self.backgroundView addSubview:blackMask];
        self.backgroundView.hidden = NO;
        
        if (lastScreenShotView) [lastScreenShotView removeFromSuperview];
        
        UIImage *lastScreenShot = [self.screenShotsList lastObject];
        lastScreenShotView = [[UIImageView alloc]initWithImage:lastScreenShot];
        
        startBackViewX = startX;
        [lastScreenShotView setFrame:CGRectMake(startBackViewX,
                                                lastScreenShotView.frame.origin.y,
                                                lastScreenShotView.frame.size.height,
                                                lastScreenShotView.frame.size.width)];
        [lastScreenShotView setBackgroundColor:[UIColor blueColor]];
        
        [self.backgroundView insertSubview:lastScreenShotView belowSubview:blackMask];
    }
    
    
    
    x = x>PMW?PMW:x;
    x = x<0?0:x;
    
    frame = self.view.frame;
    frame.origin.x = x;
    self.view.frame = frame;
    
    float alpha = 0.4 - (x/800);
    
    blackMask.alpha = alpha;
    
    CGFloat aa = abs(startBackViewX)/kkBackViewWidth;
    CGFloat y = x*aa;
    
    CGFloat lastScreenShotViewHeight = kkBackViewHeight;
    
    //TODO: FIX self.edgesForExtendedLayout = UIRectEdgeNone  SHOW BUG
    /**
     *  if u use self.edgesForExtendedLayout = UIRectEdgeNone; pls add
     
     if (!iOS7) {
     lastScreenShotViewHeight = lastScreenShotViewHeight - 20;
     }
     *
     */
    [lastScreenShotView setFrame:CGRectMake(startBackViewX+y,
                                            0,
                                            kkBackViewWidth,
                                            lastScreenShotViewHeight)];
    
}



-(BOOL)isBlurryImg:(CGFloat)tmp
{
    return YES;
}

#pragma mark - Gesture Recognizer -

- (void)paningGestureReceive:(UIPanGestureRecognizer *)recoginzer
{
    if (self.viewControllers.count <= 1 || !self.canDragBack){
        
        [self setNvcCanDragBack:NO];
        return;
    };
    
    CGPoint touchPoint = [recoginzer locationInView:KEY_WINDOW];
    
    if (recoginzer.state == UIGestureRecognizerStateBegan) {
        
        self.isMoving = YES;
        startTouch = touchPoint;
        
        
        [self.backgroundView  removeFromSuperview];
        self.backgroundView = nil;
        
        
    }else if (recoginzer.state == UIGestureRecognizerStateEnded){
        
        if (touchPoint.x - startTouch.x > 50)
        {
            [UIView animateWithDuration:0.3 animations:^{
                [self moveViewWithX:PMW];
            } completion:^(BOOL finished) {
                
                [self popViewControllerAnimated:NO];
                CGRect frame = self.view.frame;
                frame.origin.x = 0;
                self.view.frame = frame;
                
                self.isMoving = NO;
            }];
        }
        else
        {
            [UIView animateWithDuration:0.3 animations:^{
                [self moveViewWithX:0];
            } completion:^(BOOL finished) {
                self.isMoving = NO;
                self.backgroundView.hidden = YES;
            }];
            
        }
        return;
        
    }else if (recoginzer.state == UIGestureRecognizerStateCancelled){
        
        [UIView animateWithDuration:0.3 animations:^{
            [self moveViewWithX:0];
        } completion:^(BOOL finished) {
            self.isMoving = NO;
            self.backgroundView.hidden = YES;
        }];
        
        return;
    }
    
    if (self.isMoving) {
        [self moveViewWithX:touchPoint.x - startTouch.x];
    }
}



- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (![viewController isKindOfClass:[MainViewController class]]) {
        [self setNvcCanDragBack:YES];
    }else{
        [self setNvcCanDragBack:NO];
    }
    
    if (self.viewControllers.count <= 1 || !self.canDragBack){
        [self setNvcCanDragBack:NO];
    };
    
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}



@end


